﻿package 
{
    import Scrollbar.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;

    public class Scrollbar extends MovieClip
    {
        public var _ScrollIcon:MovieClip;
        public var _DownBtn:SimpleButton;
        public var _ScrollBtn:MovieClip;
        public var _UpBtn:SimpleButton;
        public var _ListBack:MovieClip;
        private var _filterColor:ColorMatrixFilter;
        private var _filters:Array;
        private var _target:MovieClip;
        private var _targetBool:Boolean = false;
        private var _targetY:Number;
        private var _firstBool:Boolean = false;
        private var _isTargetY:Boolean = false;
        private var _showWidth:Number = 0;
        private var _widthBool:Boolean = false;
        private var _mainHeight:Number = 0;
        private var _isMainHeight:Boolean = false;
        private var _isShowBottom:Boolean = false;
        private var _noRefult:Boolean = false;
        private var _showHeight:Number = 0;
        private var _heightBool:Boolean = false;
        private var _moveNum:Number = 0;
        private var _speed:int = 5;
        private var _scrollbarX:int = 0;
        private var _scrollbarXBool:Boolean = false;
        private var _scrollbarY:int = 0;
        private var _scrollbarYBool:Boolean = false;
        private var _maskX:int = 0;
        private var _maskXBool:Boolean = false;
        private var _maskY:int = 0;
        private var _maskYBool:Boolean = false;
        private var _appointLive:Number;
        private var _visibleScrollbar:Boolean = true;
        private var _rect:Sprite;
        private var _rectBool:Boolean = false;
        private var _wheel:Sprite;
        private var _wheelBool:Boolean = false;
        private var _showBottomFirst:Boolean = false;
        private var _loadThisBool:Boolean = false;
        private var ratio:Number = 0;
        private var _move:Number = 0;
        private var _movePoint:Number = 0;
        private var _isMove:Boolean = false;
        private var _isLostUp:Boolean = false;
        private var _isLostDown:Boolean = false;

        public function Scrollbar()
        {
            this._filterColor = new ColorMatrixFilter([0.2, 0.2, 0.2, 0, 0, 0.2, 0.2, 0.2, 0, 0, 0.2, 0.2, 0.2, 0, 0, 0, 0, 0, 1, 1]);
            this._filters = new Array();
            trace("滚动条版本0.2");
            this._ScrollBtn.y = 0;
            this._ScrollIcon.mouseEnabled = false;
            this._ListBack.alpha = 0.54;
            this._filters.push(this._filterColor);
            return;
        }// end function

        public function set target(param1:MovieClip) : void
        {
            if (this._target != param1)
            {
                if (this._target != null)
                {
                    this.clear();
                }
            }
            if (param1 == null)
            {
                this.scrollUpClear();
            }
            this._target = param1;
            if (this._target == null)
            {
            }
            if (this._firstBool)
            {
                this.close();
                return;
            }
            if (this._target == null)
            {
                return;
            }
            if (this._isMainHeight == false)
            {
                this._mainHeight = this._target.height;
            }
            if (this._maskYBool == false)
            {
            }
            if (this._isTargetY == false)
            {
            }
            if (this._firstBool == false)
            {
                this._targetY = this._target.y;
                this._firstBool = true;
            }
            this._targetBool = true;
            this.startList();
            return;
        }// end function

        public function set targetY(param1:Number) : void
        {
            this._targetY = param1;
            this._isTargetY = true;
            return;
        }// end function

        public function set showWidth(param1:Number) : void
        {
            this._showWidth = param1;
            this._widthBool = true;
            this.startList();
            return;
        }// end function

        public function set mainHeight(param1:Number) : void
        {
            this._isMainHeight = true;
            this._mainHeight = param1;
            return;
        }// end function

        public function get mainHeight() : Number
        {
            return this._mainHeight;
        }// end function

        public function set showBottom(param1:Boolean) : void
        {
            this._isShowBottom = param1;
            return;
        }// end function

        public function set noRefult(param1:Boolean) : void
        {
            this._noRefult = param1;
            return;
        }// end function

        public function set showHeight(param1:Number) : void
        {
            this._showHeight = param1;
            this._heightBool = true;
            this.startList();
            return;
        }// end function

        public function set moveNum(param1:Number) : void
        {
            this._moveNum = param1;
            return;
        }// end function

        public function set speed(param1:int) : void
        {
            this._speed = param1;
            return;
        }// end function

        public function set liveY(param1:int) : void
        {
            this._ScrollBtn.y = param1;
            this.scrollMove();
            return;
        }// end function

        public function set scrollbarX(param1:int) : void
        {
            this._scrollbarX = param1;
            this._scrollbarXBool = true;
            return;
        }// end function

        public function set scrollbarY(param1:int) : void
        {
            this._scrollbarY = param1;
            this._scrollbarYBool = true;
            return;
        }// end function

        public function set maskX(param1:int) : void
        {
            this._maskX = param1;
            this._maskXBool = true;
            return;
        }// end function

        public function set maskY(param1:int) : void
        {
            this._maskY = param1;
            this._maskYBool = false;
            return;
        }// end function

        public function set appointMc(param1:Number) : void
        {
            this._appointLive = param1 - this._showHeight;
            if (this._targetBool)
            {
            }
            if (this._target)
            {
            }
            if (this._appointLive > 0)
            {
                this._ScrollBtn.y = this._appointLive * this._movePoint;
                this.scrollMove();
            }
            return;
        }// end function

        public function set visibleScrollbar(param1:Boolean) : void
        {
            this._visibleScrollbar = param1;
            return;
        }// end function

        public function set listBackAlpha(param1:Number) : void
        {
            this._ListBack.alpha = param1;
            return;
        }// end function

        private function createMask() : void
        {
            this.clearMask();
            if (this._maskXBool == false)
            {
                this._maskX = this._target.x;
            }
            if (this._maskYBool == false)
            {
                this._maskY = this._targetY;
            }
            this._rect = new Sprite();
            this._rect.graphics.beginFill(0);
            this._rect.graphics.drawRect((this._maskX - 1), this._maskY, this._showWidth + 2, this._showHeight);
            this._rect.graphics.endFill();
            this._rect.mouseEnabled = false;
            this._target.parent.addChild(this._rect);
            this._target.mask = this._rect;
            this._rectBool = true;
            return;
        }// end function

        private function createWheel() : void
        {
            this.clearWheel();
            this._wheel = new Sprite();
            this._wheel.graphics.beginFill(0);
            this._wheel.graphics.drawRect(0, 0, this._target.width, this._mainHeight);
            this._wheel.graphics.endFill();
            this._target.addChildAt(this._wheel, 0);
            this._target.addEventListener(MouseEvent.MOUSE_WHEEL, this.onScrollWheel);
            this._wheel.alpha = 0;
            this._wheelBool = true;
            return;
        }// end function

        private function clearMask() : void
        {
            if (this._target)
            {
            }
            if (this._target.parent)
            {
            }
            if (this._rectBool)
            {
                this._target.mask = null;
                this._target.parent.removeChild(this._rect);
                this._rect = null;
                this._rectBool = false;
            }
            return;
        }// end function

        private function clearWheel() : void
        {
            if (this._target)
            {
            }
            if (this._wheelBool)
            {
            }
            if (this._wheel.parent)
            {
                this._wheel.parent.removeChild(this._wheel);
                this._target.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onScrollWheel);
                this._wheel = null;
                this._wheelBool = false;
            }
            return;
        }// end function

        private function startList() : void
        {
            if (this._targetBool != false)
            {
            }
            if (this._heightBool != false)
            {
            }
            if (this._widthBool == false)
            {
                return;
            }
            this.clear();
            this.loadScrollbar();
            this.scrollbarLive();
            this.scrollbarSize();
            if (this.ratio >= 1)
            {
                this.renderData();
                this.hideBtn(false);
                this._DownBtn.filters = this._filters;
                this._UpBtn.filters = this._filters;
                this._ListBack.visible = this._visibleScrollbar;
                this._DownBtn.visible = this._visibleScrollbar;
                this._UpBtn.visible = this._visibleScrollbar;
                this._showBottomFirst = true;
            }
            else
            {
                this.createMask();
                this.createWheel();
                this.hideBtn(true);
                this._DownBtn.filters = null;
                this._UpBtn.filters = null;
                this._ListBack.visible = true;
                this._DownBtn.visible = true;
                this._UpBtn.visible = true;
                if (this._isShowBottom)
                {
                    if (this._showBottomFirst == true)
                    {
                        this._ScrollBtn.y = this._move;
                    }
                    if (this._ScrollBtn.y > this._move - 10)
                    {
                        this._ScrollBtn.y = this._move;
                    }
                }
                else
                {
                    if (this._noRefult)
                    {
                    }
                    if (this._showBottomFirst == false)
                    {
                    }
                }
                this.scrollMove();
            }
            return;
        }// end function

        private function hideBtn(param1:Boolean) : void
        {
            this._ScrollBtn.visible = param1;
            this._ScrollIcon.visible = param1;
            return;
        }// end function

        private function loadScrollbar() : void
        {
            if (this._loadThisBool == false)
            {
                this._target.parent.addChild(this);
                this.eventListener();
                this._loadThisBool = true;
            }
            return;
        }// end function

        private function removeScrollbar() : void
        {
            if (this._loadThisBool)
            {
            }
            if (this.parent)
            {
                this.parent.removeChild(this);
                this._loadThisBool = false;
            }
            return;
        }// end function

        private function scrollbarLive() : void
        {
            if (this._scrollbarXBool == true)
            {
                this.x = this._scrollbarX;
            }
            else
            {
                this.x = this._target.x + this._showWidth;
            }
            if (this._scrollbarYBool == true)
            {
                this.y = this._scrollbarY;
            }
            else
            {
                this.y = this._targetY + this._UpBtn.height;
            }
            return;
        }// end function

        private function scrollbarSize() : void
        {
            this._ListBack.height = this._showHeight - this._DownBtn.height * 2;
            this._DownBtn.y = this._ListBack.height;
            this.ratio = this._showHeight / this._mainHeight;
            this._ScrollBtn.height = this._ListBack.height * this.ratio;
            this._move = this._ListBack.height - this._ScrollBtn.height;
            if (this._move <= 0)
            {
                this._movePoint = 0;
            }
            else
            {
                this._movePoint = this._move / (this._mainHeight + this._moveNum - this._showHeight);
            }
            this.range();
            this._ScrollIcon.y = this._ScrollBtn.y + this._ScrollBtn.height / 2;
            return;
        }// end function

        private function range() : void
        {
            if (this._ScrollBtn.y > this._move)
            {
                this._ScrollBtn.y = this._move;
            }
            if (this._ScrollBtn.y >= 0)
            {
            }
            if (this.ratio >= 1)
            {
                this._ScrollBtn.y = 0;
            }
            this.scrollMove();
            return;
        }// end function

        private function eventListener() : void
        {
            this._UpBtn.addEventListener(MouseEvent.CLICK, this.onUpBtn);
            this._ScrollBtn.addEventListener(MouseEvent.MOUSE_DOWN, this.onScrollDown);
            this._DownBtn.addEventListener(MouseEvent.CLICK, this.onDownBtn);
            return;
        }// end function

        private function removeListener() : void
        {
            this._UpBtn.removeEventListener(MouseEvent.CLICK, this.onUpBtn);
            this._ScrollBtn.removeEventListener(MouseEvent.MOUSE_DOWN, this.onScrollDown);
            this._DownBtn.removeEventListener(MouseEvent.CLICK, this.onDownBtn);
            return;
        }// end function

        private function onScrollWheel(event:MouseEvent) : void
        {
            if (event.delta > 0)
            {
                this.upMove();
            }
            else
            {
                this.downMove();
            }
            return;
        }// end function

        private function onScrollDown(event:MouseEvent) : void
        {
            this._showBottomFirst = false;
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onScrollUp, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.onScrollMove, false, 0, true);
            this._ScrollBtn.startDrag(false, new Rectangle(0, 0, 0, this._move));
            this._isMove = true;
            return;
        }// end function

        private function onScrollUp(event:MouseEvent) : void
        {
            this.scrollUpClear();
            return;
        }// end function

        private function scrollUpClear() : void
        {
            this._ScrollBtn.stopDrag();
            if (stage == null)
            {
                return;
            }
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.onScrollUp);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.onScrollMove);
            this._isMove = false;
            return;
        }// end function

        private function onScrollMove(event:MouseEvent) : void
        {
            this._isLostDown = false;
            this._isLostUp = false;
            this.scrollMove();
            return;
        }// end function

        private function onUpBtn(event:MouseEvent) : void
        {
            this.upMove();
            return;
        }// end function

        private function onDownBtn(event:MouseEvent) : void
        {
            this.downMove();
            return;
        }// end function

        private function upMove() : void
        {
            this._showBottomFirst = false;
            if (this._ScrollBtn.y - this._speed < 0)
            {
                this._ScrollBtn.y = 0;
            }
            else
            {
                this._ScrollBtn.y = this._ScrollBtn.y - this._speed;
            }
            this.scrollMove();
            return;
        }// end function

        private function downMove() : void
        {
            this._showBottomFirst = false;
            if (this._ScrollBtn.y + this._speed > this._move)
            {
                this._ScrollBtn.y = this._move;
            }
            else
            {
                this._ScrollBtn.y = this._ScrollBtn.y + this._speed;
            }
            this.scrollMove();
            return;
        }// end function

        private function scrollMove() : void
        {
            if (stage == null)
            {
                return;
            }
            if (this._movePoint <= 0)
            {
                this._target.y = this._targetY;
            }
            else
            {
                this._target.y = -this._ScrollBtn.y / this._movePoint + this._targetY;
            }
            this._ScrollIcon.y = this._ScrollBtn.y + this._ScrollBtn.height / 2;
            return;
        }// end function

        private function close() : void
        {
            if (this._isMove)
            {
                this.scrollUpClear();
            }
            this._firstBool = false;
            this._showBottomFirst = true;
            this.clear();
            this.removeListener();
            this.removeScrollbar();
            this._target = null;
            return;
        }// end function

        private function clear() : void
        {
            this.clearMask();
            this.clearWheel();
            return;
        }// end function

        private function renderData() : void
        {
            this._ScrollBtn.y = 0;
            this.scrollMove();
            return;
        }// end function

    }
}
